//管理设备数据相关
import {defineStore} from "pinia";
import {ref} from "vue";
import {
    useGetAllEquipmentAPI,
    useGetAllCountAPI,
    useGetByTypeAPI,
    useDeleteOneByIdAPI,
    useByMultifunctionAPI, useModifyStatusAPI,
    useGetCountByTypeNameAPI, useGetCountByMultifunctionNameAPI, useGetOneByIdAPI
} from "@/apis/equipment.ts";
import {RouteParamValue} from "vue-router";

export const useEquipmentStore=defineStore('equipment',()=>{

        //1.定义菜单的数据的state
        const allEquipmentByPage=ref([]);

        const equipmentCount=ref(0);

        const equipmentData=ref({});


        // 分页获取所有数据
        const getEquipmentByPage=async (current:number,pageSize:number)=>{
            const res=await useGetAllEquipmentAPI(current,pageSize);
            allEquipmentByPage.value=res.data;
        }

        // 获取所有数据行总数
        const getEquipmentCount=async ()=>{
            const res=await useGetAllCountAPI();
            equipmentCount.value=res.data;

        }

        //  根据设备类型查询设备数据
        const getEquipmentByType=async (typeName: string, orderBy: number, current: number, pageSize: number)=>{
            const res=await  useGetByTypeAPI(typeName,orderBy,current,pageSize);
            allEquipmentByPage.value=res.data;
            equipmentCount.value=(await useGetCountByTypeNameAPI(typeName)).data;
        }


        const deleteOneById=async (id:number)=>{
            await useDeleteOneByIdAPI(id);
        }


        // todo 多功能查询接口 count还是有问题
        const getByMultifunction=async (name:string,current:number,pageSize:number)=>{
            const res=await useByMultifunctionAPI(name,current,pageSize);
            allEquipmentByPage.value=res.data;
            equipmentCount.value=(await useGetCountByMultifunctionNameAPI(name)).data;
        }

        // 修改一条信息状态
        const modifyStatus=async (formData:FormData)=>{
           await useModifyStatusAPI(formData);
        }
        // 根据id获取一条数据

    const getOneByIdStore=async (id: string | RouteParamValue[])=>{
          const res= await useGetOneByIdAPI(id);
          equipmentData.value=res.data;
    }





        //3.以对象的格式把state和action return
        return {
            allEquipmentByPage,getEquipmentByPage,equipmentCount,getEquipmentCount
            ,getEquipmentByType,deleteOneById,
            getByMultifunction,modifyStatus,getOneByIdStore,equipmentData
        }
    }

)